{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# LGBM → PMML\n",
    "\n",
    "### Exporter: LGBM Classifier\n",
    "### Data Set used: Iris \n",
    "\n",
    "\n",
    "### **STEPS**: \n",
    "- Build the Pipeline with preprocessing \n",
    "- Build PMML using Nyoka exporter"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Pre-processing, Model building (using pipeline) for Iris data set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-13T17:23:34.023366Z",
     "start_time": "2018-08-13T17:23:33.043866Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Pipeline(memory=None,\n",
       "     steps=[('scaling', StandardScaler(copy=True, with_mean=True, with_std=True)), ('LGBMC_preprocess', LGBMClassifier(boosting_type='gbdt', class_weight=None, colsample_bytree=1.0,\n",
       "        learning_rate=0.1, max_depth=-1, min_child_samples=20,\n",
       "        min_child_weight=0.001, min_split_gain=0.0, n_estimators=5,\n",
       "        n_jobs=-1, num_leaves=31, objective=None, random_state=None,\n",
       "        reg_alpha=0.0, reg_lambda=0.0, silent=True, subsample=1.0,\n",
       "        subsample_for_bin=200000, subsample_freq=0))])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sklearn import datasets\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.preprocessing import StandardScaler, Imputer\n",
    "from lightgbm import LGBMRegressor,LGBMClassifier\n",
    "\n",
    "iris = datasets.load_iris()\n",
    "irisd = pd.DataFrame(iris.data,columns=iris.feature_names)\n",
    "irisd['Species'] = iris.target\n",
    "\n",
    "features = irisd.columns.drop('Species')\n",
    "target = 'Species'\n",
    "\n",
    "pipeline_obj = Pipeline([\n",
    "    ('scaling',StandardScaler()), \n",
    "    ('LGBMC_preprocess',LGBMClassifier(n_estimators=5))\n",
    "])\n",
    "\n",
    "pipeline_obj.fit(irisd[features],irisd[target])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Export the Pipeline object into PMML using the Nyoka package"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-13T17:23:36.956866Z",
     "start_time": "2018-08-13T17:23:36.319366Z"
    }
   },
   "outputs": [],
   "source": [
    "from nyoka import lgb_to_pmml\n",
    "lgb_to_pmml(pipeline_obj,features,target,\"lgbmc_pmml_preprocess.pmml\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
